package cn.edu.nju.software.action.user;

import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import cn.edu.nju.software.bean.show.UserShow;
import cn.edu.nju.software.service.intf.UserService;
import cn.edu.nju.software.util.Constants;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class ModifyUserPasswordAction extends ActionSupport implements SessionAware {

	private String oldPassword;
	private String newPassword;
	private UserShow user;
	private UserService userService;
	private Map<String, Object> session;

	public void setOldPassword(String oldPassword) {
		this.oldPassword = oldPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}
	
	@Override
	public void validate() {
		this.clearFieldErrors();
		user = (UserShow) session.get(Constants.SESSION_USER);
		if (!user.getPassword().equals(oldPassword)) {
			this.addFieldError("ModifyAdminPasswordAction", "原密码错误！");
		}
	}

	@Override
	public String execute() throws Exception {
		user.setPassword(newPassword);
		userService.updateUser(user.getUser());
		return SUCCESS;
	}

}
